package com.system.service;

import java.util.ArrayList;
import java.util.List;

import com.system.dao.CourseDao;
import com.system.dao.DaoFactory;
import com.system.dao.ScoreDao;
import com.system.dao.StudentDao;
import com.system.dao.TeacherDao;
import com.system.json.JsonContext;
import com.system.model.CourseScorePojo;
import com.system.model.Score;
import com.system.model.ScorePojo;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class ScoreService {
	private DaoFactory factory = DaoFactory.getInstance();
	private JsonContext context = new JsonContext();
	/**
	 * 按班级查询学生成绩
	 * @param clazz
	 * @param major
	 * @return
	 */
	public JSONObject checkoutQueryScores(String clazz,String major){
		ScoreDao dao = factory.createScoreDao();
		List<ScorePojo> list = dao.listScoresbyClassMajor(clazz,major);
		//System.out.println(list);
		dao=factory.createScoreDao();
		List<String> numList=dao.listStunums(clazz, major);
		if(list.size()==0)
			return context.getFailedObject("查询结果为空");
		else{
			List<List<ScorePojo>> all = new ArrayList<List<ScorePojo>>();
			for(int i=0;i<numList.size();i++){
				String stunum=numList.get(i);
				List<ScorePojo> tmp = new ArrayList();
				for(int j=0;j<list.size();j++){
					if(list.get(j).getStunum().equals(stunum))
						tmp.add(list.get(j));
				}
				all.add(tmp);
			}
			List<JSONObject> arr = new ArrayList();
			for (int i = 0; i < all.size(); i++) {
				List<ScorePojo> jsonlist = all.get(i);
				JSONObject obj = new JSONObject();
				obj.put("stuname", jsonlist.get(0).getStuname());
				obj.put("stuclass", jsonlist.get(0).getStuclass());
				int sum=0;
				for (ScorePojo score : jsonlist) {
					//System.out.println(score.getCoursename());
					obj.put(score.getCoursename(), Integer.parseInt(score.getScore()));
					sum+=Integer.parseInt(score.getScore());
				}
				if(jsonlist.size()!=4){
					obj.put("java", 0);
					obj.put("python",0);
					obj.put("Cpp", 0);
					obj.put("CSharp", 0);
					obj.put("sum", 0);
				}
				else
					obj.put("sum", sum+"");
				arr.add(obj);
			}
			JSONArray jsonArray=JSONArray.fromObject(arr);
			return context.getSuccessObject("查询成功", null, jsonArray);
		}
	}
	/**
	 * 为学生成绩查询增加分页信息
	 * @param clazz
	 * @param major
	 * @param pageNum
	 * @param amount
	 * @return
	 */
	public JSONObject checkoutQueryScoresbyPage(String clazz,String major,int pageNum,int amount){
		ScoreDao dao = factory.createScoreDao();
		List<ScorePojo> list = dao.listScoresbyClassMajor(clazz,major);
		//System.out.println(list);
		dao=factory.createScoreDao();
		List<String> numList=dao.listStunums(clazz, major);
		if(list.size()==0)
			return context.getFailedObject("查询结果为空");
		else{
			List<List<ScorePojo>> all = new ArrayList<List<ScorePojo>>();
			for(int i=0;i<numList.size();i++){
				String stunum=numList.get(i);
				List<ScorePojo> tmp = new ArrayList();
				for(int j=0;j<list.size();j++){
					if(list.get(j).getStunum().equals(stunum))
						tmp.add(list.get(j));
				}
				all.add(tmp);
			}
			List<JSONObject> arr = new ArrayList();
			for (int i = 0; i < all.size(); i++) {
				List<ScorePojo> jsonlist = all.get(i);
				JSONObject obj = new JSONObject();
				obj.put("stuname", jsonlist.get(0).getStuname());
				obj.put("stuclass", jsonlist.get(0).getStuclass());
				int sum=0;
				for (ScorePojo score : jsonlist) {
					//System.out.println(score.getCoursename());
					obj.put(score.getCoursename(), Integer.parseInt(score.getScore()));
					sum+=Integer.parseInt(score.getScore());
				}
				if(jsonlist.size()!=4){
					obj.put("java", 0);
					obj.put("python",0);
					obj.put("Cpp", 0);
					obj.put("CSharp", 0);
					obj.put("sum", 0);
				}
				else
					obj.put("sum", sum+"");
				arr.add(obj);
			}
			JSONArray jsonArray=JSONArray.fromObject(arr);
			if(jsonArray.size()<pageNum*amount){
				return context.getSuccessObject("查询成功", null,JSONArray.fromObject(jsonArray.subList((pageNum-1)*amount, jsonArray.size())));
			}
			else{
				return context.getSuccessObject("查询成功", null, JSONArray.fromObject(jsonArray.subList((pageNum-1)*amount, pageNum*amount)));
			}
		}
			
		
	}
	/**
	 * 录入成绩
	 * @param teanum
	 * @param stunum
	 * @param coursename
	 * @param Score
	 * @return
	 */
	public JSONObject checkoutInsertScore(String teanum,String stunum,String coursename,Integer stu_score){
		TeacherDao tdao=factory.createTeacherDao();
		ScoreDao sdao = factory.createScoreDao();
		StudentDao ssdao=factory.createStuDao();
		CourseDao cdao=factory.createCourseDao();
		int tea_id=tdao.findId(teanum);
		int course_id=cdao.findId(coursename, tea_id);
		int stu_id=ssdao.findId(stunum);
		Score score=new Score();
		score.setTea_id(tea_id);score.setStu_id(stu_id);score.setCourse_id(course_id);score.setScore(stu_score);
		if(sdao.add(score)){
			return context.getSuccessObject("录入成功", null, null);
		}
		else{
			return context.getFailedObject("录入失败，数据库异常");
		}
		
	}
	/**
	 * 教师查询自己所教课程的学生成绩
	 * @param teanum
	 * @return
	 */
	public JSONObject checkoutQueryTeaScores(String teanum){
		TeacherDao tdao=factory.createTeacherDao();
		ScoreDao sdao = factory.createScoreDao();
		Integer tea_id=tdao.findId(teanum);
		List<CourseScorePojo> list=sdao.listScoresbyteaid(tea_id);
		if(list.isEmpty())
			return context.getFailedObject("查询结果为空");
		else{
			JSONArray arr=JSONArray.fromObject(list);
			return context.getSuccessObject("查询成功", null, arr);
		}
	}
	
}
